import {defineStore} from 'pinia';
import router from '@/router';
import MenuConfig from '@/config/menu';
function getMenuList(data, parentPath) {
    if(Helper.emptyArray(data)) {
        return [];
    }
    let menuList = [];
    data.forEach((d) => {
        if(d.menuType == 1) {
            d.url = d.path;
            if(! Helper.empty(parentPath)) {
                d.url = parentPath + d.path;
            }
            d.type = "c"
            // d.visible = 0
            d.component = d.componentPath;
            menuList.push(d);
        } else {
            d.type = "m";
            // d.visible = 0;
            menuList = menuList.concat(getMenuList(d.children, d.path));
        }
    })
    return menuList;
}

const store = {
    id: "menu",
    state(){
        return {
            menuList: [],
            history: [],
            cacheList: [],
        }
    },

    getters: {

    },

    actions: {
        async init() {
            // const router = useRouter();

            let routers = router.getRoutes();
            let idx = routers.findIndex(x => x.name == 'Enter');
            let root = routers[idx];
            let {code, data} = await Http.get('/menu').auth(true);
            // let code = 200;
            // let data = MenuConfig;
            if(code == 200 && ! Helper.emptyArray(data)) {
                let menuList = getMenuList(data);
                const viewsComponent = import.meta.glob('@/views/**/*.vue',{eager: false});
                menuList.forEach(menu => {
                    //({meta: {history: true, title: "菜单管理"}, path: '/sys/menu', name: 'menu', component: () => import('@/views/sys/menu/Index.vue')})
                    let componentName = menu.component.replace(new RegExp("/", 'g'), "-")
                        .toLowerCase().substring(1)
                    root.children.push({
                        meta:{history: menu.visible == 0, cache: menu.isCache == 1 ? true : false, title: menu.name},
                        path: menu.url,
                        name: componentName,
                        // component: () => import('@/views/' + menu.component + ".vue")
                        component: viewsComponent['/src/views' + menu.component + '.vue']
                    })
                })

                console.log(root);
                router.addRoute(root);
                this.setMenuList(data);
            }
        },

        async setMenuList(menuList) {
            this.menuList = menuList;
        },

        addHistory(menu) {
            let index = this.history.findIndex(h => menu.path == h.path);
            if(index === -1) {
                let meta = menu.meta ? menu.meta : {};
                if(meta.cache && this.cacheList.indexOf(menu.name) === -1) {
                    this.cacheList.push(menu.name);
                }
                menu.width = meta.title.length * 16 + 30;
                menu.meta = meta;
                this.history.push(menu);
            }
        },

        removeHistory(menu) {
            let index = this.history.findIndex(h => menu.path == h.path);
            if(index > -1) {
                this.history.splice(index, 1);
            }
            this.removeCache(menu);
            return index;
        },

        getHistory() {
            return this.history;
        },

        clearHistory() {
            this.history = [];
            this.cacheList = [];
        },

        removeCache(menu) {
            let index = this.cacheList.indexOf(menu.name);
            if(index > -1) {
                this.cacheList.splice(index, 1);
            }
            return index;
        }
    }
}

export default defineStore(store)
